from bitarray import bitarray


class DataBitmap(object):

    def __init__(self, max= 10**8):
        self.max = max
        self.array = bitarray(self.max)
        self.array.setall(0)

    def add(self, dataStr):
        if not self.contains(dataStr):
            num = self.str_hash(dataStr)
            self.array[num] = 1

    def remove(self, dataStr):
        if self.contains(dataStr):
            num = self.str_hash(dataStr)
            self.array[num] = 0

    def contains(self, dataStr):
        num = self.str_hash(dataStr)
        return self.array[num]

    def bit_count(self):
        return self.array.count(1)

    def str_hash(self, dataStr):
        # BKDRHash
        seed = 131313
        hash = 0
        for ch in dataStr:
            hash = hash * seed + ord(ch)
        return hash % self.max